<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>维护页面</title>
    <link rel="stylesheet" href="/res/layui/css/layui.css"  media="all">
    <script src="/res/js/jquery-3.4.1.min.js"></script>
    <script src="/res/js/tools.js"></script>
</head>
<body>
<form class="layui-form" action="" lay-filter="example" id="myForm">
    <input type="hidden" name="fid">
    <div class="layui-form-item">
        <label class="layui-form-label">鲜花名称</label>
        <div class="layui-input-inline">
            <input type="text" name="fname" required lay-verify="required" placeholder="请输入鲜花名字" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">鲜花价格</label>
        <div class="layui-input-inline">
            <input type="text" name="fprice"  placeholder="请输入鲜花价格" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">鲜花数量</label>
        <div class="layui-input-inline">
            <input type="text" name="fquantity" placeholder="请输入鲜花数量" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">上架日期</label>
        <div class="layui-input-inline">
            <input type="text" id="data" name="fdate"  placeholder="请选择上架日期" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">鲜花类型</label>
        <div class="layui-input-block">
            <select name="lid" id="lid" lay-verify="">

            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">鲜花照片</label>
        <div class="layui-input-block">
            <<input type="file" name="fqimg">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="parent.layer.closeAll()">关闭</button>
        </div>
    </div>
</form>
<script src="/res/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,layer = layui.layer
            ,layedit = layui.layedit
            ,laydate = layui.laydate;
        var $=layui.$;

        //日期
        laydate.render({
            elem: '#data'
        });

        //自定义验证规则
        form.verify({
            fName: function(value){
                if(value.length < 2){
                    return '名字至少得2个字符啊';
                }
            }
        });
        //监听提交
        form.on('submit(formDemo)', function(data){
            let forData=new FormData(document.getElementById("myForm"));

            let url="/flTb/addOrUpdate";
            console.log(url);
            layer.msg("入土");
            console.log(forData);
            $.ajax({
            	async:true, //true 异步 false 同步
            	type:"POST",//请求方式
            	url:url,//请求路径
            	data:forData,//请求参数
                contentType:false,
            	processData:false,
            	dataType:"json",//返回类型
            	success:function (resp){//成功回调参数
                    if (resp.state=="1"){
                        //在子页面调用父页面的方法
                        console.log("取到====================")
                        parent.closeLayer(resp.msg);
                    }else{
                        layer.msg(resp.msg);
                    }
            	},error:function (){//失败回调参数

            	}
            });
            return false;
        });
        let url='../lvTb/flowerTypeList';
        $.get(url,null,function (resp){
            console.info("================================================================")
            console.log(resp.data);
            $.each(resp.data, function (index, item) {
                console.log(item.lname);
                $("#lid").append("<option value='" + item.lid + "'>" + item.lname + "</option>>")
            });
            fuzhi();
            //重新渲染
            form.render();
        },"json");


        fuzhi();
        function fuzhi(){
            console.log("调用=================")
            let fId=getQueryString("id");
            console.info(fId);
            if (fId){
                let url_="/flTb/selectOne";
                let data_={id:fId};
                $.post(url_,data_,function (resp){
                    console.info("resp",resp);
                    form.val('example',
                        {
                            fid:resp.data.fid,
                            fname:resp.data.fname,
                            fprice:resp.data.fprice,
                        }
                    );

                },"json");
            }

        }

    });

</script>
    </body>
</html>